package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.*;
import com.hqyj.zss.services.DetailOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/detailOrder")
public class DetaiilOrderController {

    private DetailOrderService detailOrderService;
    private List<Menus> list = new ArrayList<>();

    @Autowired
    public void setDetailOrderService(DetailOrderService detailOrderService) {
        this.detailOrderService = detailOrderService;
    }

    @RequestMapping("/deleteList")
    public String deletelist(String mename){
        for (int i = 0 ; i < list.size() ; i++){
            String name = list.get(i).getMenusName();
            if (mename == name){
                list.remove(i);
                return "redirect:getShopCar";
            }
        }
        return "";
    }

    @RequestMapping("/getShopCar")
    public ModelAndView getShop(){
//        List<ShopCar> alist = detailOrderService.getAllShop();
        return new ModelAndView("shopcar","itemList", list);
    }

    @RequestMapping("/insertState")
    public String insertState(String name){
        Menus menus = detailOrderService.getByMenuName(name);
//        ShopCar shopCar = new ShopCar(menus.getMenusName(), menus.getMenusPrice(), 1);
//        detailOrderService.inserShop(shopCar);
        list.add(menus);
        return "";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insertIntoOrder() throws ParseException{
        Seat seat = new Seat();
        List<DishOrder> listDishOrder =new ArrayList<>();
        listDishOrder.add(new DishOrder(1,"青椒肉丝炒饭",1,12.00,"到店",0));
        listDishOrder.add(new DishOrder(1,"扬州炒饭",1,13.00,"到店",0));
        Date utilDate = new Date();//util.Date
        Timestamp sqlDate = new Timestamp(utilDate.getTime());//util.Date转sql.Date
        ArrayList arrayList = new ArrayList();
        for (DishOrder dishOrder : listDishOrder){
            if(dishOrder.getDish_Method()=="到店"){
                /*插入order订单*/
                //获取位置信息列表
                /*更新座位信息*/
                List<Seat> listSeat = detailOrderService.getSeat();
                //更新位置信息
                if (!arrayList.contains(dishOrder.getDish_Id())){
                    //获取第一个位置信息
                    seat = listSeat.get(0);
                    arrayList.add(dishOrder.getDish_Id());
                    seat.setSeat_State(0);
                    detailOrderService.updateSeat(seat);
                }

                List<Employee> listEmployee = detailOrderService.getEmployee();
                int employeeId = listEmployee.get(0).getE_id();
                //创建新待插入的order对象
                Order order = new Order(dishOrder.getDish_Id(),employeeId, seat.getSeat_Id(), sqlDate,0,dishOrder.getDish_State());
                //插入order
//                detailOrderService.updateOrder(order);
                detailOrderService.insertOrder(order);
                System.out.println("order自增ID为："+order.getO_Id());

//                /*插入detailOrder订单*/
                Menus menus = detailOrderService.getByMenuName(dishOrder.getDish_Name());
                DetailOrder detailOrder = new DetailOrder(order.getO_Id(),menus.getMenusId(), menus.getMenusName(), dishOrder.getDish_Num(),dishOrder.getDish_Price());
                detailOrderService.insertDetailOrder(detailOrder);
                System.out.println("detailOrder自增Id为："+detailOrder.getDo_Id());

            }else {
                //创建新待插入的order对象
                Order order = new Order(dishOrder.getDish_Id(), sqlDate,0,dishOrder.getDish_State());
                System.out.println(order.toString());
                detailOrderService.insertOrder(order);

                /*插入detailOrder订单*/
                Menus menus = detailOrderService.getByMenuName(dishOrder.getDish_Name());
                DetailOrder detailOrder = new DetailOrder(order.getO_Id(),menus.getMenusId(), menus.getMenusName(), dishOrder.getDish_Num(),dishOrder.getDish_Price());
                detailOrderService.insertDetailOrder(detailOrder);
            }
        }
        list.clear();
        return "";
    }
}
